/**
 * Created by jiangtao on 16/6/21.
 */
define(function(require, exports, module) {
    var util = {};
    //图片上传
    util.uploadImage = (function($){
        var UploadImage = function(options){
           var option = {
               el:'body',
               url:"",
               size:5,
               before:function(){},
               success:function(){},
               error:function(){}
           };
           this.options = window.mergeData(options,option);
           this.init();
        };
        UploadImage.prototype = {
            init:function(){
                var self = this;
                var el = document.querySelector(this.options.el);
                window.removeEvent(el,'change',handel);
                window.bindEvent(el,'change',handel);
                function handel(e){
                    var file = e.target.files[0];
                    if(!file || !self.isImage(file)) return;
                    self.uploadSend(file,e.target);
                };
            },
            isImage:function(file){
                 var fileName = file.name;
                 var fileSize = file.size;
                 var isImage = /\.(jpg|jpeg|png|JPG|PNG)$/.test(fileName);
                 if(!isImage) {
                     alert('请上传图片');
                     return false;
                 };
                 if(fileSize > this.options.size * 1024 * 1024) {
                     alert('上传图片不能大于' + this.options.size + 'M');
                     return false;
                 };
                 return true;
            },
            uploadSend:function(file,taget){
                taget ? $(taget).val(""):"";

                var self = this;

                this.options.before();

                // FormData 对象

                var form = new FormData();

                form.append("author", "hooyes");

                form.append("file", file);

                // XMLHttpRequest 对象

                var xhr = new XMLHttpRequest();

                xhr.open("post", self.options.url , true);

                xhr.onload = function (data) {
                    var target = data.target;
                    if(target.readyState == 4 && target.status == 200) {
                        self.options.success(JSON.parse(target.response.toString()));
                    } else {
                        self.options.error(target);
                    };
                };

                xhr.send(form);
            }
        };
        return function uploadImage(options){
            return new UploadImage(options);
        };
    })();
    //随机数
    util.getRandomStr = function(len){
        var len = len || 32;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = $chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        };
        return pwd;
    };

    //select控件
    util.select = function(){
       var selectWrap = document.querySelectorAll('.input') || [];
       for(var i = 0 ; i < selectWrap .length ; i ++){
          var nodes = selectWrap[i].childNodes || [];
          for(var j = 0 ; j < nodes.length ; j ++ ){
             if(nodes[j].tagName == "SELECT") {
                 removeEvent(nodes[j],'change',handel);
                 bindEvent(nodes[j],'change',handel);
             };
          };
       };
       function handel(){
           var value = this.value;
           var text  = this.options[this.options.selectedIndex].text;
           var nodes = this.parentNode.childNodes;
           for(var i = 0 ; i < nodes.length ; i ++ ){
               if(nodes[i].className == "select_view") {
                   nodes[i].innerText = text;
               };
           };
       };
    };
    module.exports = util;

});


